package com.gm.controller;
import com.gm.bean.UserInfo;
import com.gm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @Author Administrator
 * @Date 2020/6/23 9:06
 **/
@Controller
public class LoginController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/user/login")
    public String doLogin(@RequestParam("nickname") String nickname,
                          @RequestParam("password") String password,
                          HttpSession session, Model model) {
        UserInfo user = this.userMapper.getUserByName(nickname);
        if (user != null) {
            if (user.getPassword().equals(password)) {
                System.out.println("登陆成功！");
                session.setAttribute("user", user);
                return "redirect:/main.html";
            } else {
                System.out.println("密码错误！");
                model.addAttribute("msg", "密码错误!");
                return "login";
            }
        } else {
            System.out.println("用户不存在！");
            model.addAttribute("msg", "用户名不存在！");
            return "login";
        }
    }
}
